package mm.chenchen.script;

import lombok.extern.slf4j.Slf4j;
import mm.chenchen.constant.ResultCode;
import mm.chenchen.utils.ObjectUtils;

import java.io.*;

@Slf4j
public class ScriptUtils {


    public static int exec(String exec) {

        Process proc;
        try {
            //proc = Runtime.getRuntime().exec("C:\\Users\\dev\\Desktop\\chenchen-fast-start\\chenchen-fast-start\\script\\python\\venv\\Scripts\\python.exe" +
            //        " C:\\Users\\dev\\Desktop\\chenchen-fast-start\\chenchen-fast-start\\script\\python\\venv\\Include\\study\\study1.py");// 执行py文件
            log.info("开始执行脚本语句: [{}] ", exec);
            //String[] xx=new String[] {"nohup" ,"python3",  "/opt/scripts/python/yuepiao/yuepiao.py", "&2>1"};
            //proc= Runtime.getRuntime().exec(xx);
            //proc= Runtime.getRuntime().exec(exec);
            Runtime rt = Runtime.getRuntime();

            try {
                Process p = rt.exec(exec);
                // 防止缓冲区满, 导致卡住
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        String line;
                        try {
                            BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                            while ((line = stderr.readLine()) != null) {
                                System.out.println("stderr:" + line);
                            }
                        } catch (Exception e) {

                        }

                    }
                }.start();


                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        String line;
                        try {
                            BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
                            while ((line = stdout.readLine()) != null) {
                                System.out.println("stdout:" + line);
                            }
                        } catch (Exception e) {

                        }
                    }
                }.start();


                int exitVal = p.waitFor();
                if (0 != exitVal) {
                    System.out.println("执行脚本失败");
                }
                System.out.println("执行脚本成功");

            } catch (Exception e) {
                e.printStackTrace();
            }

            //用输入输出流来截取结果
//            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
//            StringBuffer execResult=new StringBuffer();
//            String line = null;
//            while ((line = in.readLine()) != null) {
//                //System.out.println(line);
//                log.info("[{}] ",line);
//                execResult.append(line);
//            }
//            in.close();
            //proc.waitFor();
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.FAILURE;
        }

    }

    public static int exec(String exec,String logFilePath) {
        File f=new File(logFilePath);
        BufferedWriter out = null;


        Process proc;
        try {

            //proc = Runtime.getRuntime().exec("C:\\Users\\dev\\Desktop\\chenchen-fast-start\\chenchen-fast-start\\script\\python\\venv\\Scripts\\python.exe" +
            //        " C:\\Users\\dev\\Desktop\\chenchen-fast-start\\chenchen-fast-start\\script\\python\\venv\\Include\\study\\study1.py");// 执行py文件
            log.info("开始执行脚本语句: [{}] ", exec);
            //String[] xx=new String[] {"nohup" ,"python3",  "/opt/scripts/python/yuepiao/yuepiao.py", "&2>1"};
            //proc= Runtime.getRuntime().exec(xx);
            //proc= Runtime.getRuntime().exec(exec);
            Runtime rt = Runtime.getRuntime();


            try {
                Process p = rt.exec(exec);
                // 防止缓冲区满, 导致卡住
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        String line;
                        try {
                            BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                            while ((line = stderr.readLine()) != null) {
                                if (!ObjectUtils.isEmpty(logFilePath)){

                                    RandomAccessFile randomFile = new RandomAccessFile(f, "rw");
                                    long fileLength = randomFile.length();
                                    randomFile.seek(fileLength);
                                    randomFile.write(line.getBytes());
                                    randomFile.writeBytes("\n");
                                    randomFile.close();
                                    //System.out.println("stdout:" + line);
                                }
                            }
                        } catch (Exception e) {

                        }

                    }
                }.start();


                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        String line;
                        try {
                            BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
                            while ((line = stdout.readLine()) != null) {
                                if (!ObjectUtils.isEmpty(logFilePath)){
                                    RandomAccessFile randomFile = new RandomAccessFile(f, "rw");
                                    long fileLength = randomFile.length();
                                    randomFile.seek(fileLength);
                                    randomFile.write(line.getBytes());
                                    randomFile.writeBytes("\n");
                                    randomFile.close();
                                    //System.out.println("stdout:" + line);
                                }
                            }
                        } catch (Exception e) {

                        }
                    }
                }.start();



                int exitVal = p.waitFor();
                if (0 != exitVal) {
                    log.info("执行脚本失败");
                }else {
                    log.info("执行脚本成功");

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            //用输入输出流来截取结果
//            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
//            StringBuffer execResult=new StringBuffer();
//            String line = null;
//            while ((line = in.readLine()) != null) {
//                //System.out.println(line);
//                log.info("[{}] ",line);
//                execResult.append(line);
//            }
//            in.close();
            //proc.waitFor();
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.FAILURE;
        }

    }
}
